package com.salt.modules.sys.web;

import com.salt.common.json.AjaxJson;
import com.salt.common.utils.StringUtils;
import com.salt.core.log.annotation.OperationRecord;
import com.salt.core.persistence.Page;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.Admin;
import com.salt.modules.sys.service.AdminService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description
 * @Author salt
 * @Date 2022/1/12
 * @Version 1.0.1
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/admin")
public class AdminController extends BaseController {

    private final AdminService adminService;

    @ModelAttribute("admin")
    public Admin get(@RequestParam(required = false) String id){
        if (StringUtils.isNotBlank(id)) {
            return adminService.get(id);
        }
        return new Admin();
    }

    @GetMapping("page")
    public AjaxJson page(Admin admin, HttpServletRequest request, HttpServletResponse response) {
        Page<Admin> page = adminService.findPage(new Page<>(request, response), admin);
        return AjaxJson.success().put("page", page);
    }

    @PostMapping("save")
    @OperationRecord(service = "系统服务", module = "管理员管理", operation = "新增/修改", description = "新增/修改管理员")
    public AjaxJson save(Admin admin) {
        admin.setTenant(admin.getUser().getCompany().getId());
        adminService.save(admin);
        return AjaxJson.success("保存成功!");
    }

    @GetMapping("queryById")
    public AjaxJson queryById(Admin admin) {
        return AjaxJson.success().put("admin", admin);
    }

    @DeleteMapping("delete")
    @OperationRecord(service = "系统服务", module = "管理员管理", operation = "删除", description = "删除管理员")
    public AjaxJson delete(Admin admin) {
        adminService.delete(admin);
        return AjaxJson.success("删除成功!");
    }
}
